<template>
	<gui-page>
		<template v-slot:gBody>
			<view 
			style="margin-top:66rpx;" 
			class="gui-padding-x">
				<text 
				class="gui-h6 gui-color-gray">{{devname}}-{{devtype}}-设备id={{devid}}</text>
				<text
				v-if="rtid > 0" class="gui-h6 gui-color-gray">; 数据id={{rtid}}</text>
			</view>
			<view 
			class="gui-bg-white gui-dark-bg-level-3 gui-padding gui-margin-top">
				<view v-for="(item, index) in items" @click="gotoRTDataField(item)">
					<cardBody :icon="'&#xe685;'" :title="`${item.title}`" :value="`${item.ctbbxx>1? item.value * item.ctbbxx:item.value}`" :arrowShow="'1'"></cardBody>
				</view>	
			</view>
		</template>
	</gui-page>
</template>

<script setup>
	import { ref,getCurrentInstance,createApp } from 'vue'
	import { onLoad,onPullDownRefresh } from "@dcloudio/uni-app"
	import cardBody from "@/lib/components/card-body.vue"
	import guiPage from "@/Grace6/components/gui-page.vue"
	import ybmethod from '../../lib/js/ybmethod'
	import ybgolbal from '../../lib/js/ybgolbal'
	const app = createApp();
	const ybapi = ybgolbal.getybapi(getCurrentInstance());
	let mc = ref("");
	let devid = ref(0);
	let xlh = ref("");
	let devname = ref("");
	let devtype = ref("");
	let item = ref({});
	let items = ref([]);
	let rtid = ref(0);
	let ctbb = ref(0);
	
	onLoad((option)=>{
		console.log("进入实时数据页面");
		console.log(option);
		devid.value = option.devid;
		mc.value = option.mc;
		xlh.value = option.xlh;
		devname.value = option.devname;
		devtype.value = option.devtype;
		rtid.value = option.rtid;
		ctbb.value = option.ctbb;
		getData();
	})
	
	onPullDownRefresh(()=>{
		ybmethod.onPullDownRefresh(getData);
	})
	
	function getData(){
		console.log("执行")
		if(rtid.value){
			console.log("执行")
			ybapi.Devices_GetRTDataByid(rtid.value,xlh.value,function(data){   // 接口有问题，报服务器运行错误: Api::$devid undefined
				console.log(data)
				if(data.total>0){
					item.value = data.items[0];
					fillitems();
				}
			},function(e){
				console.log(e);
			});	
		}else{
			console.log("devid==="+devid.value)
			console.log(xlh.value)
			ybapi.Devices_GetRTData(devid.value,xlh.value,"id desc","*",1,1,function(data){
				console.log(data)
				if(data.total>0){
					item.value = data.items[0];
					fillitems();					
				}	
			},function(e){
				console.log(e);
			});	
		}
	}
	
	function gotoRTDataField(item){
		console.log(item);
		// 跳转数据域
		if(item.name === "update_time"){
			
		}else{
			console.log(rtid.value)
			uni.navigateTo({
				url:"/pages/rtdatafield/rtdatafield?devid="+devid.value+"&ctbb="+ctbb.value+"&ctbbxx="+item.ctbbxx+"&rtid="+rtid.value+"&field="+item.name+"&xlh="+xlh.value+"&devname="+devname.value+"&mc="+mc.value+"&devtype="+devtype.value+"&fieldtitle="+item.title,
			})
		}
	}
	
	function fillitems(){
		console.log("获取最新数据")
		console.log(item.value);
		if(item.value){
			console.log(JSON.parse(JSON.stringify(item.value)))
			let newitem = JSON.parse(JSON.stringify(item.value));
			rtid.value = newitem.id;
			console.log(newitem);
			console.log(xlh.value);
			if(xlh.value=="db"){
				items.value.push({ctbbxx:1,title:"更新时间",name:"update_time",value:newitem.update_time});
				items.value.push({ctbbxx:1,title:"A相电压(V)",name:"ua",value:newitem.ua});
				items.value.push({ctbbxx:1,title:"B相电压(V)",name:"ub",value:newitem.ub});
				items.value.push({ctbbxx:1,title:"C相电压(V)",name:"uc",value:newitem.uc});
				items.value.push({ctbbxx:ctbb.value,title:"A相电流(A)",name:"ia",value:newitem.ia});
				items.value.push({ctbbxx:ctbb.value,title:"B相电流(A)",name:"ib",value:newitem.ib});
				items.value.push({ctbbxx:ctbb.value,title:"C相电流(A)",name:"ic",value:newitem.ic});
				items.value.push({ctbbxx:ctbb.value,title:"总有功功率(W)",name:"pyz",value:newitem.pyz});
				items.value.push({ctbbxx:ctbb.value,title:"A相有功功率(W)",name:"pya",value:newitem.pya});
				items.value.push({ctbbxx:ctbb.value,title:"B相有功功率(W)",name:"pyb",value:newitem.pyb});
				items.value.push({ctbbxx:ctbb.value,title:"C相有功功率(W)",name:"pyc",value:newitem.pyc});
				items.value.push({ctbbxx:ctbb.value,title:"总无功功率(Var)",name:"pwz",value:newitem.pwz});
				items.value.push({ctbbxx:ctbb.value,title:"A相无功功率(Var)",name:"pwa",value:newitem.pwa});
				items.value.push({ctbbxx:ctbb.value,title:"B相无功功率(Var)",name:"pwb",value:newitem.pwb});
				items.value.push({ctbbxx:ctbb.value,title:"C相无功功率(Var)",name:"pwc",value:newitem.pwc});
				items.value.push({ctbbxx:ctbb.value,title:"总视在功率(VA)",name:"pszz",value:newitem.pszz});
				items.value.push({ctbbxx:ctbb.value,title:"A相视在功率(VA)",name:"psza",value:newitem.psza});
				items.value.push({ctbbxx:ctbb.value,title:"B相视在功率(VA)",name:"pszb",value:newitem.pszb});
				items.value.push({ctbbxx:ctbb.value,title:"C相视在功率(VA)",name:"pszc",value:newitem.pszc});
				items.value.push({ctbbxx:1,title:"总功率因数",name:"ysz",value:newitem.ysz});
				items.value.push({ctbbxx:1,title:"A相功率因数",name:"ysa",value:newitem.ysa});
				items.value.push({ctbbxx:1,title:"B相功率因数",name:"ysb",value:newitem.ysb});
				items.value.push({ctbbxx:1,title:"C相功率因数",name:"ysc",value:newitem.ysc});
				items.value.push({ctbbxx:1,title:"A相频率(HZ)",name:"hza",value:newitem.hza});
				items.value.push({ctbbxx:1,title:"B相频率(HZ)",name:"hzb",value:newitem.hzb});
				items.value.push({ctbbxx:1,title:"C相频率(HZ)",name:"hzc",value:newitem.hzc});
				items.value.push({ctbbxx:ctbb.value,title:"总有功电量(Kwh)",name:"zygdl",value:newitem.zygdl});
				
				items.value.push({ctbbxx:ctbb.value,title:"总有功总尖电量(Kwh)",name:"zygdlzj",value:newitem.zygdlzj});
				items.value.push({ctbbxx:ctbb.value,title:"总有功总峰电量(Kwh)",name:"zygdlzf",value:newitem.zygdlzf});
				items.value.push({ctbbxx:ctbb.value,title:"总有功总平电量(Kwh)",name:"zygdlzp",value:newitem.zygdlzp});
				items.value.push({ctbbxx:ctbb.value,title:"总有功总谷电量(Kwh)",name:"zygdlzg",value:newitem.zygdlzg});
				items.value.push({ctbbxx:ctbb.value,title:"总有功总深电量(Kwh)",name:"zygdlzs",value:newitem.zygdlzs});
				
				items.value.push({ctbbxx:ctbb.value,title:"总有功电量正向(Kwh)",name:"zygdlz",value:newitem.zygdlz});
				items.value.push({ctbbxx:ctbb.value,title:"总有功电量反向(Kwh)",name:"zygdlf",value:newitem.zygdlf});
				items.value.push({ctbbxx:ctbb.value,title:"总无功电量(Kvarh)",name:"zwgdl",value:newitem.zwgdl});
				
				items.value.push({ctbbxx:ctbb.value,title:"总无功总尖电量(Kvarh)",name:"zwgdlzj",value:newitem.zwgdlzj});
				items.value.push({ctbbxx:ctbb.value,title:"总无功总峰电量(Kvarh)",name:"zwgdlzf",value:newitem.zwgdlzf});
				items.value.push({ctbbxx:ctbb.value,title:"总无功总平电量(Kvarh)",name:"zwgdlzp",value:newitem.zwgdlzp});
				items.value.push({ctbbxx:ctbb.value,title:"总无功总谷电量(Kvarh)",name:"zwgdlzg",value:newitem.zwgdlzg});
				items.value.push({ctbbxx:ctbb.value,title:"总无功总深电量(Kvarh)",name:"zwgdlzs",value:newitem.zwgdlzs});
				
				items.value.push({ctbbxx:ctbb.value,title:"总无功电量正向(Kvarh)",name:"zwgdlz",value:newitem.zwgdlz});
				items.value.push({ctbbxx:ctbb.value,title:"总无功电量反向(Kvarh)",name:"zwgdlf",value:newitem.zwgdlf});
			}
			if(xlh.value=="wgbc"){
				items.value.push({ctbbxx:1,title:"更新时间",name:"update_time",value:newitem.update_time});
				items.value.push({ctbbxx:1,title:"A相超前标志",name:"cqbza",value:newitem.cqbza});
				items.value.push({ctbbxx:1,title:"A相功率因素",name:"ysa",value:newitem.ysa});
				items.value.push({ctbbxx:1,title:"A相电压(V)",name:"ua",value:newitem.ua});
				items.value.push({ctbbxx:1,title:"A相电流(A)",name:"ia",value:newitem.ia});
				items.value.push({ctbbxx:1,title:"A相电压谐波(%)",name:"uaxb",value:newitem.uaxb});
				items.value.push({ctbbxx:1,title:"A相电流谐波(%)",name:"iaxb",value:newitem.iaxb});
				items.value.push({ctbbxx:1,title:"A相有功功率(W)",name:"yga",value:newitem.yga});
				items.value.push({ctbbxx:1,title:"A相无功功率(Var)",name:"wga",value:newitem.wga});
				items.value.push({ctbbxx:1,title:"A相频率(HZ)",name:"hza",value:newitem.hza});
				
				items.value.push({ctbbxx:1,title:"B相超前标志",name:"cqbzb",value:newitem.cqbzb});
				items.value.push({ctbbxx:1,title:"B相功率因素",name:"ysb",value:newitem.ysb});
				items.value.push({ctbbxx:1,title:"B相电压(V)",name:"ub",value:newitem.ub});
				items.value.push({ctbbxx:1,title:"B相电流(A)",name:"ib",value:newitem.ib});
				items.value.push({ctbbxx:1,title:"B相电压谐波(%)",name:"ubxb",value:newitem.ubxb});
				items.value.push({ctbbxx:1,title:"B相电流谐波(%)",name:"ibxb",value:newitem.ibxb});
				items.value.push({ctbbxx:1,title:"B相有功功率(W)",name:"ygb",value:newitem.ygb});
				items.value.push({ctbbxx:1,title:"B相无功功率(Var)",name:"wgb",value:newitem.wgb});
				items.value.push({ctbbxx:1,title:"B相频率(HZ)",name:"hzb",value:newitem.hzb});
				
				items.value.push({ctbbxx:1,title:"C相超前标志",name:"cqbzc",value:newitem.cqbzc});
				items.value.push({ctbbxx:1,title:"C相功率因素",name:"ysc",value:newitem.ysc});
				items.value.push({ctbbxx:1,title:"C相电压(V)",name:"uc",value:newitem.uc});
				items.value.push({ctbbxx:ctbb.value,title:"C相电流(A)",name:"ic",value:newitem.ic});
				items.value.push({ctbbxx:1,title:"C相电压谐波(%)",name:"ucxb",value:newitem.ucxb});
				items.value.push({ctbbxx:1,title:"C相电流谐波(%)",name:"icxb",value:newitem.icxb});
				items.value.push({ctbbxx:ctbb.value,title:"C相有功功率(W)",name:"ygc",value:newitem.ygc});
				items.value.push({ctbbxx:ctbb.value,title:"C相无功功率(Var)",name:"wgc",value:newitem.wgc});
				items.value.push({ctbbxx:1,title:"C相频率(HZ)",name:"hzc",value:newitem.hzc});
				items.value.push({ctbbxx:1,title:"投切标志",name:"tqbz",value:newitem.tqbz});
			}
			console.log(items.value)
		}else{
			return ;
		}
	}
	
	app.component({
		cardBody,
		guiPage
	});
</script>

<style>
	       
</style>
